package com.kaiwukj.android.ufamily.mvp.ui.page.dynamic.topic;

import android.content.Intent;
import android.os.Bundle;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.blankj.utilcode.util.StringUtils;
import com.kaiwukj.android.mcas.di.component.AppComponent;
import com.kaiwukj.android.ufamily.R;
import com.kaiwukj.android.ufamily.app.base.review.BaseMvpActivity;
import com.kaiwukj.android.ufamily.app.constant.ARouterUrlKt;
import com.kaiwukj.android.ufamily.di.component.DaggerDynamicComponent;
import com.kaiwukj.android.ufamily.di.module.DynamicModule;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.TopicResult;
import com.kaiwukj.android.ufamily.mvp.ui.page.dynamic.DynamicContract;
import com.kaiwukj.android.ufamily.mvp.ui.widget.SpaceItemDecoration;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

@Route(path = ARouterUrlKt.TopicPickUrl)
public class TopicPickActivity extends BaseMvpActivity<TopicPresenter> implements DynamicContract.TopicView {

    @BindView(R.id.refresh_layout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.rv_list)
    RecyclerView rvList;
    @BindView(R.id.et_search)
    EditText etSearch;


    private TopicPickAdapter mPickAdapter;

    @OnClick(R.id.container_left)
    void onBackClick() {
        onBackPressedSupport();
    }

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        super.setupActivityComponent(appComponent);
        DaggerDynamicComponent.builder()
                .appComponent(appComponent)
                .dynamicModule(new DynamicModule(this))
                .build().inject(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_dynamic_topic_pick;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        etSearch.setHint("添加合适的自定义标签");
        etSearch.setOnEditorActionListener((v, actionId, event) -> {
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                String keyword = etSearch.getText().toString().trim();
                mPresenter.fetchTopic(keyword);
                return true;
            }
            return false;
        });
        refreshLayout.setEnableLoadMore(false);
        refreshLayout.setOnRefreshListener(refreshLayout -> loadData());
        rvList.setLayoutManager(new LinearLayoutManager(this));
        rvList.addItemDecoration(new SpaceItemDecoration(1));
        mPickAdapter = new TopicPickAdapter();

        rvList.setAdapter(mPickAdapter);

        mPickAdapter.setOnItemClickListener((adapter, view, position) -> {
            TopicResult topicResult = mPickAdapter.getItem(position);
            if (topicResult.getId() == -1) {
                mPresenter.addTopic(topicResult.getTopicTitle());
            } else {
                pickCallback(topicResult);
            }
        });
    }


    private void loadData() {
        String keyword = etSearch.getText().toString().trim();
        mPresenter.fetchTopic(keyword);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        loadData();
    }

    @Override
    public void onFetchTopic(List<TopicResult> results) {
        mPickAdapter.setList(results);
        String keyword = etSearch.getText().toString().trim();
        if (!StringUtils.isEmpty(keyword)) {
            if (!isContainTitle(keyword, results)) {
                TopicResult result = new TopicResult();
                result.setId(-1);
                result.setTopicTitle(keyword);
                mPickAdapter.addData(0, result);
            }
        }
    }

    @Override
    public void onTopicAdd(Integer topicId) {
        String topicTitle = etSearch.getText().toString().trim();
        TopicResult topicResult = new TopicResult();
        topicResult.setId(topicId);
        topicResult.setTopicTitle(topicTitle);
        pickCallback(topicResult);
    }

    @Override
    public void onGetTopic(TopicResult result) {

    }

    @Override
    public void onTopicGroupJoin(String hxGroupId) {

    }

    private void pickCallback(TopicResult result) {
        Bundle data = new Bundle();
        data.putSerializable("topic", result);

        Intent intent = new Intent();
        intent.putExtras(data);
        setResult(RESULT_OK, intent);
        finish();
    }

    private boolean isContainTitle(String title, List<TopicResult> results) {
        for (TopicResult result : results) {
            if (title.equals(result.getTopicTitle())) return true;
        }
        return false;
    }
}
